﻿/*
   Copyright 2012 Dmitry Bratus

   Licensed under the Apache License, Version 2.0 (the "License");
   you may not use this file except in compliance with the License.
   You may obtain a copy of the License at

       http://www.apache.org/licenses/LICENSE-2.0

   Unless required by applicable law or agreed to in writing, software
   distributed under the License is distributed on an "AS IS" BASIS,
   WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
   See the License for the specific language governing permissions and
   limitations under the License.
*/
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using NUnit.Framework;
using Jurassic;

namespace Flower.Directory.Scripting.Tests
{
    [TestFixture]
    public class XmlObjectTest
    {
        [Test]
        public void PrimitiveTypesToFromXml()
        {
            var engine = Setup.CreateEngine();

            engine.Execute("var boolXml = boolToXml(true);");
            Assert.IsTrue(engine.Evaluate<bool>("boolXml.asBool()"));

            engine.Execute("boolXml.asBool(false);");
            Assert.IsFalse(engine.Evaluate<bool>("boolXml.asBool()"));

            engine.Execute("var intXml = intToXml(1);");
            Assert.IsTrue(engine.Evaluate<int>("intXml.asInt()") == 1);

            engine.Execute("intXml.asInt(2);");
            Assert.IsTrue(engine.Evaluate<int>("intXml.asInt()") == 2);

            engine.Execute("var doubleXml = doubleToXml(1.5);");
            Assert.IsTrue(engine.Evaluate<double>("doubleXml.asDouble()") == 1.5);

            engine.Execute("doubleXml.asDouble(2.5);");
            Assert.IsTrue(engine.Evaluate<double>("doubleXml.asDouble()") == 2.5);

            engine.Execute("var stringXml = stringToXml('aaa');");
            Assert.IsTrue(engine.Evaluate<string>("stringXml.asString()") == "aaa");

            engine.Execute("stringXml.asString('bbb');");
            Assert.IsTrue(engine.Evaluate<string>("stringXml.asString()") == "bbb");
        }

        [Test]
        public void XPath()
        {
            const string xml = @"<root xmlns:a=""xml-namespace-a""><a>text1</a><b>text2</b><a:c>text3</a:c></root>";
            
            var engine = Setup.CreateEngine();

            engine.SetGlobalValue("testXml", xml);

            engine.Execute("$xmlns.a = 'xml-namespace-a';");

            engine.Execute("var nodes = xml(testXml).xpath('/root/a:c');");
            Assert.IsTrue(engine.Evaluate<int>("nodes.length") == 1);

            engine.Execute("nodes = xml(testXml).xpath('/root/b');");
            Assert.IsTrue(engine.Evaluate<int>("nodes.length") == 1);
        }

        [Test]
        public void Attributes()
        {
            const string xml = @"<root><a></a></root>";

            var engine = Setup.CreateEngine();

            engine.SetGlobalValue("testXml", xml);

            engine.Execute("var xDoc = xml(testXml);");
            engine.Execute("var xNode = xDoc.xpath('/root/a')[0];");

            engine.Execute("xNode.attr('a1', 'a1_text').attr('a2', 'a2_text');");

            Assert.AreEqual("a1_text", engine.Evaluate<string>("xNode.attr('a1')"));
            Assert.AreEqual("a2_text", engine.Evaluate<string>("xNode.attr('a2')"));
        }

        [Test]
        public void InnerText()
        {
            const string xml = @"<root><a></a></root>";

            var engine = Setup.CreateEngine();

            engine.SetGlobalValue("testXml", xml);

            engine.Execute("var xDoc = xml(testXml);");
            engine.Execute("var xNode = xDoc.xpath('/root/a')[0];");

            Assert.AreEqual(true, engine.Evaluate<bool>("xNode.innerText('text') == xNode"));
            Assert.AreEqual("text", engine.Evaluate<string>("xNode.innerText()"));
        }

        [Test]
        public void AppendRemove()
        {
            const string xml = @"<root></root>";

            var engine = Setup.CreateEngine();

            engine.SetGlobalValue("testXml", xml);
            
            engine.Execute("var xDoc = xml(testXml);");
            engine.Execute("var xNode = xDoc.xpath('/root')[0];");

            engine.Execute("xNode.append(xml('<a></a>')).append(xml('<b></b>'));");

            Assert.AreEqual(2, engine.Evaluate<int>("xNode.childrenCount()"));

            engine.Execute("xNode.xpath('/root/a')[0].remove();");

            Assert.AreEqual(1, engine.Evaluate<int>("xNode.childrenCount()"));
        }

        [Test]
        public void Insertions()
        {
            const string xml = @"<root></root>";

            var engine = Setup.CreateEngine();

            engine.SetGlobalValue("testXml", xml);
            engine.Execute("var xDoc = xml(testXml);");
            engine.Execute("var xNode = xDoc.xpath('/root')[0];");

            engine.Execute("xNode.append(xml('<a>Node3</a>'))");
            engine.Execute("xNode.prepend(xml('<a>Node2</a>'))");
            engine.Execute("xNode.insertBefore(xml('<a>Node1</a>'), xNode.children()[0])");
            engine.Execute("xNode.insertAfter(xml('<a>Node4</a>'), xNode.children()[2])");

            Assert.AreEqual("Node1", engine.Evaluate<string>("xNode.children()[0].innerText()"));
            Assert.AreEqual("Node2", engine.Evaluate<string>("xNode.children()[1].innerText()"));
            Assert.AreEqual("Node3", engine.Evaluate<string>("xNode.children()[2].innerText()"));
            Assert.AreEqual("Node4", engine.Evaluate<string>("xNode.children()[3].innerText()"));
        }
    }
}
